import 'package:common/common.dart';
import 'package:common/net/app_api.dart';

/// 网络请求
class Network {
  Network._();

  // 检查Token
  static void checkToken(ValueChanged<bool> onResult) {
    HttpManager().post(AppApi.checkToken, successCallback: (result) {}, errorCallback: (error) {}, finishCallback: () {});
  }

  // 微信登录
  static void wechatLogin(String code, String state, int source, {VoidCallback? onStart, ValueChanged? onSuccess, VoidCallback? onFinish}) {
    HttpManager().get(AppApi.wechatLogin,
        params: {"code": code, "state": state, "source": source},
        startCallback: () => onStart?.call(),
        successCallback: (result) => onSuccess?.call(result),
        errorCallback: (error) => _doOnError(error),
        finishCallback: () => onFinish?.call());
  }

  // 处理异常
  static void _doOnError(HttpError error) {
    switch(error.code) {
      case NetCode.unConnected:
        break;
      case NetCode.connectionTimeOut:
        break;
      case NetCode.connectionError:
        break;
      case NetCode.sendTimeout:
        break;
      case NetCode.receiveTimeout:
        break;
      case NetCode.badCertificate:
        break;
      case NetCode.response:
        break;
      case NetCode.cancel:
        break;
      case NetCode.unknown:
        break;
      default:
        break;
    }
  }
}
